import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:sports_new/entity/vip/vip_pay_entity.dart';
import 'package:sports_new/server/api/other_api.dart';
import 'package:sports_new/utils/user.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/widgets/card.dart';
import 'package:umeng_apm_sdk/umeng_apm_sdk.dart';
import '../../entity/other/resource_entity.dart';
import '../../entity/pay/recharge_entity.dart';
import '../../server/api/coupon_api.dart';
import '../../server/api/pay_api.dart';
import '../../utils/toast_utils.dart';
import '../../widgets/pay_bottom_sheet.dart';

class VipLogic extends GetxController{
  List<ResourceEntity>? data;
  int? roomId = Get.parameters['roomId'].toInt();
  List<ResourceEntity> carousel = [];
  VipPayEntity? price;
  int _index = 0;
  final _num = (-1.0).obs;
  final ScrollController scrollController = ApmScrollController();
  final EasyRefreshController refreshController = EasyRefreshController();

  double get num {
    if (_num < 0) {
      return 0.0;
    } else if (_num > 1) {
      return 1.0;
    } else {
      return _num.value;
    }
  }

  set num(value) {
    _num.value = value;
    update();
  }

  CardStyle style = const CardStyle(
    margin: EdgeInsets.only(bottom: 8,left: 8,right: 8),
    padding: EdgeInsets.all(16));

  List<ResourceEntity> get plan => data?.filterOrNull((item) => item.button == "plan") ?? [];
  List<ResourceEntity> get right => data?.filterOrNull((item) => item.button == "right") ?? [];
  List<ResourceEntity> get discount => data?.filterOrNull((item) => item.button == "discount") ?? [];
  List<ResourceEntity> get benefit => data?.filterOrNull((item) => item.button == "benefit") ?? [];
  int get index => _index;

  set index(int value) {
    _index = value;
    update();
  }

  @override
  void onReady() {
    getData();
    super.onReady();
  }

  Future getData() async{
    data = await OtherApi.getAppList("app_vip") ?? [];
    price = await PayApi.getVipPay(roomId: roomId);
    await User.fetchUserInfo();
    update();
  }

  Future toPay() async{
    if(price?.prices.hasValue != true) return;
    Utils.onEvent("vip_open");
    var selectPrice = price!.prices![index];
    var x = double.parse(selectPrice.price!.split(RegExp(r"[\u4e00-\u9fa5]")).join());
    double money = x;
    final coupons = await CouponApi.use1Coupons(
        gold: money,scene: PayScene.vip, otherId: '',roomId: roomId);
    final PayResult? result = await Get.bottomSheet(
      isScrollControlled: true,
      PayBottomSheet(
        payInfo: PayInfo(
          price: money.toString(),
          payScene: PayScene.vip,
          buyId: selectPrice.id.toString(),
          coupons: coupons
        )
      )
    );
    if(result != null && result.toPay == PayStatus.goldPay){
      var s = await PayApi.vipOrder(
        selectPrice.id,
        couponGold: result.coupon?.gold,
        couponId: result.coupon?.id,
        roomId: roomId
      );
      if(s == true) {
        Utils.onEvent('pay_done');
        getData();
      }else{
        Utils.onEvent('vip_zfyd_qxzf');
        ToastUtils.show("${User.info?.isVip == 0?"开通":"续费"}失败");
      }
    }else if(result?.toPay == PayStatus.directPay){
      Utils.onEvent('vip_zfyd_qrzf');
      getData();
    }else{
      Utils.onEvent('vip_zfyd_qxzf');
    }
    User.fetchUserInfo(fetchFocus: false);
  }

  Future getBack(bool isHome) async{
    carousel = [];
    var list = await OtherApi.getAppList("app_lbt");
    list?.forEach((element) {
      if(element.button == "vip${isHome?"_home":""}"){
        carousel.add(element);
      }
    });
    update();
  }

  doRefresh(){
    refreshController.callRefresh();
  }
}